package com.atguigu.springbootmq.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类 发布确认 高级
 */
@Configuration
public class ConfirmConfig {


//交换机
    public static final String CONFIRM_EXCHANGE_NAME="confirm_exchange";
    //队列
    public static final String CONFIRM_EQUEUE_NAME="confirm_queue";
    //Routingkey
    public static final String CONFIRM_ROUTING_KEY="key1";
   //备份交换机
    public static final String BACKUP_EXCHANGE_NAME="backup_exchange";
    //备份队列
    public static final String BACKUP_EQUEUE_NAME="backup_queue";
    //报警队列
    public static final String WARNING_EQUEUE_NAME="warning_queue";


    //声明交换机
    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
     //   return new DirectExchange(CONFIRM_EXCHANGE_NAME);
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME).durable(true)
                .withArgument("alternate-exchange",BACKUP_EXCHANGE_NAME).build();


    }


    //声明隊列
    @Bean("confirmQueue")
    public Queue confirmQueue(){
         return QueueBuilder.durable(CONFIRM_EQUEUE_NAME).build();
       // return new Queue(CONFIRM_EQUEUE_NAME);

    }

     //绑定
    @Bean
    public Binding queueBindingExchange(@Qualifier("confirmQueue")Queue confirmQueue
    , @Qualifier("confirmExchange")DirectExchange confirmExchange){

        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
    }

    //备份交换机
    @Bean("backupExchange")
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }



    //备份队列
    @Bean("backupQueue")
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_EQUEUE_NAME).build();
        // return new Queue(CONFIRM_EQUEUE_NAME);

    }

    //报警队列
    @Bean("warningqueue")
    public Queue warningqueue(){
        return QueueBuilder.durable(WARNING_EQUEUE_NAME).build();
        // return new Queue(CONFIRM_EQUEUE_NAME);

    }


    //绑定  备份队列绑在备份交换机上面
    @Bean
    public Binding backupQueueBindingbanckuoExchange(@Qualifier("backupQueue")Queue backupQueue
            , @Qualifier("backupExchange")FanoutExchange backupExchange){

        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    //绑定  报警队列绑在备份交换机上面
    @Bean
    public Binding warningqueueBindingbanckuoExchange(@Qualifier("warningqueue")Queue warningqueue
            , @Qualifier("backupExchange")FanoutExchange backupExchange){

        return BindingBuilder.bind(warningqueue).to(backupExchange);
    }


}
